﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CustomControls;
using System.IO;

namespace UIwpf.SubForms
{
    /// <summary>
    /// Interaction logic for wndRegister.xaml
    /// </summary>
    public partial class wndRegister : CustomWindow
    {
        string UserKeysFile = @"C:\Temp\UsedKeys.txt";
        string KeyFile = @"C:\Temp\Key.txt";
        string PrivateKeyFile = @"C:\Temp\PrivateKey.txt";
        string HashCodeFile = @"C:\Temp\HashCodes.txt";
        bool bIsKeyUsed;

        string PrivateKey;

        public wndRegister()
        {
            InitializeComponent();
        }

        private void btnValidate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (StreamReader s = new StreamReader(PrivateKeyFile))
                {
                    PrivateKey = s.ReadLine();
                }

                string serialKey = txtSerialKey.Text;
                using (StreamReader sr = new StreamReader(UserKeysFile))
                {
                    string allUsedKeys = sr.ReadToEnd();
                    bIsKeyUsed = allUsedKeys.Contains(serialKey);
                }
                if (bIsKeyUsed)
                {
                    MessageBox.Show(
                            "Your serial key has been used.\nPlease contact CNA Software for new serial key.",
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Exclamation,
                            MessageBoxResult.OK);
                }
                else
                {
                    string hashCode = GenerateHashCode(PrivateKey, serialKey);
                    using (StreamReader sr = new StreamReader(HashCodeFile))
                    {
                        string allHashCodes = sr.ReadToEnd();
                        if (allHashCodes.Contains(hashCode))
                        {
                            MessageBox.Show("Registration is sucessful!");
                            using (StreamWriter sw = new StreamWriter(KeyFile))
                            {
                                sw.WriteLine(serialKey);
                            }
                            using (StreamWriter sw = new StreamWriter(UserKeysFile, true))
                            {
                                sw.WriteLine(serialKey);
                            }
                            Properties.Settings.Default.InstalledDateTime = DateTime.Now;
                            Properties.Settings.Default.Save();
                        }
                        else
                        {
                            if (MessageBox.Show(
                                    "Your serial key is invalid.\nPlease contact CNA Software for new serial key.",
                                    "Error",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Exclamation,
                                    MessageBoxResult.OK) == MessageBoxResult.OK)
                                this.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                        "Your software is illegally copied.\nPlease contact CNA Software to buy new version.",
                        "Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation,
                        MessageBoxResult.OK);
            }
        }

        private string GenerateHashCode(string privateKey, string serialKey)
        {
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            byte[] preHash = System.Text.Encoding.UTF32.GetBytes(privateKey + serialKey);
            byte[] hash = sha.ComputeHash(preHash);
            return System.Convert.ToBase64String(hash, 0, 15);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
